********************************************************************************
**	PURPOSE: Selection into CBL, create T8
**							
**	INPUTS: Long with survey
**	
**	OUTPUTS: Selection Effects Maina.xls 
**				
**	CREATED/EDITED BY: Kayla Wilding, Leah Kim, Hasan Ahamed
**
**	DATE CREATED: 
**
**	DATE LAST EDITED: 2/24/23
********************************************************************************
clear all

**Tests for Selection Effects into CBL Loans
*A. Filename and table title
	local decs 3

*B. Define the covariate regression spec options and the outcome variables of interest
	local testcontrols bfico_post fs081_400_post fs081_500_post fs081_600_post
	local outcomes scoredf ficoscore08
	local filename "Selection Effects Main"

local pana 1.post 1.op18#1.post 

foreach group in flag_randomized {
	use "$adta/Long with outcomes.dta", clear

	label var scoredf "Scored on FICO = 1"
	label var ficoscore08 "FICO Score"

	keep if `group' == 1
	
	local let a
		local loopcount 1
		foreach var in `outcomes' { 

			local controls 			// reset the control local
			local controlss 
			foreach control in `testcontrols' {
				cap drop mi`control'
				cap drop `control'stan
				*Standardize each control variable
					sum `control' if `=cond("`var'" == "ficoscore08", "flag_matched_scored_base", "flag_matched")' == 1 & !mi(`var')
					gen `control'stan = (`control' - `r(mean)')/`r(sd)'
					assert mi(`control') if mi(`control'stan)
					assert mi(`control'stan) if mi(`control')
				*Generate a missing dummy for each control and replace the missing obs with 0 for both the standardized control var and regular control var 
					gen mi`control' = (mi(`control'))
					replace `control' = 0 if mi(`control')
					replace `control'stan = 0 if mi(`control'stan)
				*create a local of standardized controls and missing dummies for the pdslasso
				local controlss `controlss' `control'stan 
			}
				cap drop __*
				xtset surveyid 
				*run the pdslasso with the standardized control list 

				pdslasso `var' `pan`let'' (`controlss'), fe robust
				local controls `e(xselected)'
				*Remove the stan to use the controls in the regressions
				local controls = subinstr("`controls'", "stan","",.)
				*Include the missing dummy for each selected control 
				noi di "controls for `group' `var'  `pan`let'' `controls'" 
				
				foreach selected in `controls' {

					if strpos("`selected'", "mi") == 1 {
						continue 
					}
					else {
						local miselected mi`selected'
						local included : list posof "`miselected'" in controls
						cap assert `included' == 0
							if !_rc {	//if the missing dummy is not in the select vars
								local controls `controls' mi`selected'
							}
							else {	//if the missing dummy is already in the selected vars, continue 
								local controls `controls'
							}
					}
				}

				*Mark the sample (with controls)
				areg `var'  `pan`let'' `controls' if `=cond("`var'" == "ficoscore08", "flag_matched_scored_base", "flag_matched")' == 1, absorb(surveyid) vce(cluster surveyid)
				tempvar controlsample
				gen `controlsample' = e(sample)

				**No controls
				areg `var'  `pan`let'' if `controlsample' == 1  & `=cond("`var'" == "ficoscore08", "flag_matched_scored_base", "flag_matched")' == 1, absorb(surveyid) vce(cluster surveyid)
		
				local obs = e(N)
				local inds = e(df_a) + 1
				sum `var' if e(sample) == 1 & index == 1 
				local mu_lhs = string(r(mean), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f") 

				unique surveyid if op18 == 1 & `controlsample' == 1 
				local numopen = r(unique)
				
				outreg2 using "$outputtables/`filename'`let'.xls", keep(1.op18#1.post) excel label nocons noobs dec(`decs') nor2 slow(8000) nonote noaster addtext(Controls Included?, "No", Number of open, `numopen', Obs, `obs', Individuals, `inds', Mean Dependent Variable at Baseline, `mu_lhs') `=cond(`loopcount' == 1,"replace","append")'

				**With controls
				areg `var'  `pan`let'' `controls' if `=cond("`var'" == "ficoscore08", "flag_matched_scored_base", "flag_matched")' == 1, absorb(surveyid) vce(cluster surveyid)

				local obs = e(N)
				local inds = e(df_a) + 1
				sum `var' if e(sample) == 1 & index == 1
				local mu_lhs = string(r(mean), "%14.`=cond("`var'" == "ficoscore08", "0", "`decs'")'f") 
			
					outreg2 using "$outputtables/`filename'`let'.xls",  append keep(1.op18#1.post) sortvar(1.op18#1.post) excel label nocons noobs dec(`decs') nor2 slow(8000) nonote noaster addtext(Controls Included?, "Yes", Number of open, `numopen', Obs, `obs', Individuals, `inds', Mean Dependent Variable at Baseline, `mu_lhs')
			local ++loopcount 
		}

}


**EOF***
